package com.dmw.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dmw.reggie.dto.DishDto;
import com.dmw.reggie.entity.Category;
import com.dmw.reggie.entity.Dish;
import com.dmw.reggie.entity.DishFlavor;
import com.dmw.reggie.mapper.DishMapper;
import com.dmw.reggie.service.CategoryService;
import com.dmw.reggie.service.DishFlavorService;
import com.dmw.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService{

    @Resource
    private DishFlavorService dishFlavorService;

    @Resource
    private CategoryService categoryService;


    /**
     * 新增菜品，同时保存口味信息
     * @param dishDto
     */
    @Override
    @Transactional // 涉及到多张表的操作，加上事务
    public void saveWithFlavor(DishDto dishDto) {
        // 将菜品数据保存到菜单表
        this.save(dishDto);

        // 获取菜品id
        Long id = dishDto.getId();

        // 将口味数据保存到口味表
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors.forEach(dishFlavor -> dishFlavor.setDishId(id));

        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据 id 查询菜品，包括口味信息和菜品类别
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavorAndCategory(Long id) {
        // 根据 id 查寻 Dish
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();

        // 拷贝
        BeanUtils.copyProperties(dish, dishDto);

        // 设置 categoryName
        Category category = categoryService.getById(dish.getCategoryId());
        dishDto.setCategoryName(category.getName());

        // 设置 flavors
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> dishFlavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(dishFlavors);
        return dishDto;
    }

    /**
     * 更新菜品
     * @param dishDto
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        // 更新菜品数据
        this.updateById(dishDto);

        // 删除原有的口味数据
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        // 新增口味数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(i -> {
            i.setDishId(dishDto.getId());
        });
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 批量删除菜品信息，包括口味数据
     * @param ids
     */
    @Override
    public void removeByIds(List<Long> ids) {
        // 删除菜品数据
        super.removeByIds(ids);

        // 删除口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(queryWrapper);
    }
}
